package com.evildog.workspace.system.application.controller;

import com.evildog.workspace.common.result.Result;
import com.evildog.workspace.common.result.ResultGenerator;
import com.evildog.workspace.dependencies.mybatis.object.BaseConvert;
import com.evildog.workspace.dependencies.mybatis.object.BaseStatusParam;
import com.evildog.workspace.dependencies.mybatis.util.QueryConditionUtils;
import com.evildog.workspace.system.application.bo.ComponentPermissionListBO;
import com.evildog.workspace.system.application.convert.ComponentPermissionConvert;
import com.evildog.workspace.system.application.model.ComponentPermissionListParam;
import com.evildog.workspace.system.application.model.ComponentPermissionParam;
import com.evildog.workspace.system.application.model.ComponentPermissionUpdateApiParam;
import com.evildog.workspace.system.application.service.ComponentPermissionService;
import com.evildog.workspace.system.application.vo.ComponentPermissionListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * componentPermission Controller
 * component(组件/按钮等)权限 组件必须依附于menu
 * </p>
 *
 * @author evildog
 * @since 2020-07-25
 */
@RestController
@RequestMapping("/component-permissions")
@AllArgsConstructor
@Api(tags = "componentPermission相关接口")
public class ComponentPermissionController {

    private ComponentPermissionService componentPermissionService;

    @PostMapping
    @ApiOperation(value = "添加接口")
    public Result<Boolean> save(@Validated @RequestBody ComponentPermissionParam componentPermissionParam) {
        return ResultGenerator.status(componentPermissionService.save(ComponentPermissionConvert.INSTANCE.convert(componentPermissionParam)));
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除接口")
    public Result<Boolean> removeById(@PathVariable("id") Long id) {
        return ResultGenerator.status(componentPermissionService.removeById(id));
    }

    @PatchMapping("/{id}/status")
    @ApiOperation(value = "更新status接口")
    public Result<Boolean> updateStatus(@PathVariable("id") Long id, @Validated @RequestBody BaseStatusParam roleParam) {
        return ResultGenerator.status(componentPermissionService.updateStatus(id, BaseConvert.INSTANCE.convert(roleParam)));
    }

    @PutMapping("/{id}")
    @ApiOperation(value = "更新接口")
    public Result<Boolean> updateById(@PathVariable("id") Long id, @Validated @RequestBody ComponentPermissionParam componentPermissionParam) {
        return ResultGenerator.status(componentPermissionService.updateById(id, ComponentPermissionConvert.INSTANCE.convert(componentPermissionParam)));
    }

    @GetMapping("/list")
    @ApiOperation(value = "查询list接口")
    public Result<List<ComponentPermissionListVO>> getPageList(ComponentPermissionListParam componentPermissionListParam) {
        List<ComponentPermissionListBO> componentPermissionListBO = componentPermissionService.getList(
                QueryConditionUtils.getQueryCondition(componentPermissionListParam),
                ComponentPermissionConvert.INSTANCE.convert(componentPermissionListParam));
        return ResultGenerator.success(ComponentPermissionConvert.INSTANCE.convert(componentPermissionListBO));
    }

    /**
     * 修改菜单关联的api 权限
     */
    @PostMapping("/{id}/api-permissions")
    public Result<Boolean> updateApiPermissions(@PathVariable("id") Long id, @RequestBody ComponentPermissionUpdateApiParam param) {
        return ResultGenerator.status(componentPermissionService.updateApiPermissions(id, ComponentPermissionConvert.INSTANCE.convert(param)));
    }

}